import time
import re
from common.pre_config_brower import chrome_brower, request_for_sales
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


class Target:
    def __init__(self, url):
        self.driver = chrome_brower(disableIMG=True, disableJS=False)
        self.home_url = url
        self.main_windows_id = None
        self.last_page = False
        self.open_web()

    #open the web pages
    def open_web(self):
        try:
            print(self.home_url)
            self.driver.get(self.home_url)
            self.main_windows_id = self.driver.window_handles[-1]
        except Exception as e:
            print("open the first page failure", e)

    # search the elements, e.g. production name , href, picture and so on
    def get_info_from_page(self):
        self.roll_to_top()
        self.driver.implicitly_wait(30)
        # time.sleep(10000)
        
        while True:
            items_num_info_list = self.driver.find_elements_by_xpath('//div[@data-test="product-card-default"]')
            items_num_per_page = len(items_num_info_list)
            print(items_num_per_page)
            if items_num_per_page == 24 or self.last_page:
                break
            else:
                self.open_web()
                self.roll_to_top()

        for item in items_num_info_list:
            produce_pic_item = item.find_element_by_xpath("div/h3/a")
            produce_url = produce_pic_item.get_attribute('href')
            print(produce_url)

            produce_name = item.find_element_by_xpath('div[@data-test="productCardBody"]//div[@class="h-display-flex"]').text
            price_value = item.find_element_by_xpath('div[@data-test="productCardBody"]//span[@data-test="product-price"]').text

            jpg_url = str()

            def load_pic_url():
                jpg_url_html = produce_pic_item.find_element_by_xpath('div').get_attribute('innerHTML')
                print(jpg_url_html) 
                jpg_url_list = re.findall('<source.*?srcset="(.*?)">', jpg_url_html, re.DOTALL)
                if jpg_url_list:
                    return jpg_url_list[0].strip().split("?wid=")[0]
            
            while True:
                jpg_url = load_pic_url()
                if jpg_url:
                    break
                self.roll_to_top(500)
          
            yield jpg_url, produce_name, price_value, produce_url

    def roll_to_top(self, interval=400):
        index = 0
        while index < 8000:
            js = "var q=document.documentElement.scrollTop=%s" % index
            self.driver.execute_script(js)
            time.sleep(1)
            index += interval

    def get_all_items(self):
        items_total_num_text = self.driver.find_element_by_xpath('//button[@data-test="select"]').text
        total_page_num = int(re.search(".*?of (\d+)", items_total_num_text).group(1))
        page_url = self.home_url

        windows_id_list = list()
        for page_num in range(total_page_num):
            self.home_url = page_url
            self.home_url += "&Nao=%s" % (page_num*24)
            print(self.home_url)
            windows_id_list.append(self.open_url_in_new_window(self.home_url))
            if page_num + 1 == total_page_num:
                self.last_page = True

            if (page_num+1) % 10 == 0 or self.last_page:
                for win_id in windows_id_list:
                    self.driver.switch_to_window(win_id)
                    if page_num + 1 == total_page_num and win_id == windows_id_list[-1]:
                        self.last_page = True
                    for infor in self.get_info_from_page():
                        yield infor
                    self.driver.close()
                windows_id_list.clear()
                self.driver.switch_to_window(self.driver.window_handles[0])

    def open_url_in_new_window(self, url):
        js = "window.open('%s')" % url
        self.driver.execute_script(js)
        return self.driver.window_handles[-1]
